 # -*- coding: utf-8 -*-

import db
reload( db)
from db import McfConnector

class Attribute():
	"""Attribute is like a property of an AE"""
	def __init__(self, libmyCF, attribute_id, attribute_type_id, AE_id, value):
		self.libmyCF = libmyCF
		self.attribute_id = attribute_id
		self.attribute_type_id = attribute_type_id
		self.AE_id = AE_id
		self.value= value


	def getAE( self):
		return self.libmyCF.ontology.all_AEs[self.AE_id]


	def getDefinition( self):
		return self.libmyCF.ontology.all_attributes_type[self.attribute_type_id].definition


	def getId( self):
		return self.attribute_id


	def getName( self):
		return self.libmyCF.ontology.all_attributes_type[self.attribute_type_id].name


	def getType( self):
		return self.libmyCF.ontology.all_attributes_type[self.attribute_type_id].getType()


	def getValue( self):
		return self.value


	def update(self, value=""):
		"""Update the attribute on the database"""
		if( value != ""): self.value = value
		#update mysql database
		query="UPDATE Attributes SET value='"+ self.value +"' WHERE attribute_id = " +  str(self.attribute_id)
		sql= self.libmyCF.connector.mcfQueryExec(query)
		if sql == True:
			#update local ontology
			for att in self.libmyCF.ontology.all_attributes:
				if att.getId() == self.attribute_id:
					att.value = self.value
					break
			#updated_Attribute = {self.attribute_id : Attribute( libmyCF=self.libmyCF, attribute_id=self.attribute_id, attribute_type_id=self.attribute_type_id, AE_id=self.AE_id, value=self.value)}
			#self.libmyCF.ontology.all_attributes.update(updated_AE)   
		return sql
